// app.js
// 1. 引入并执行utils.js文件
import './utils/utils'
// 2. 引入并执行http.js文件
import './utils/http'

App({
  //--------全局的数据-begin-----------
  token: '',
  refresh_token: '',
  //---------全局的数据-end----------
  onLaunch() {
    this.getToken()
    this.getRefreshToken()
  },
  // 1. 从本地获取token
  getToken() {
    wx.getStorage({
      key: 'token',
      success: (res) => {
        // 用箭头函数, 让this指向当前实例对象
        this.token = res.data
      },
    })
  },

  getRefreshToken() {
    wx.getStorage({
      key: 'refresh_token',
      success: (res) => {
        // 用箭头函数, 让this指向当前实例对象
        this.refresh_token = res.data
      },
    })
  },

  // 2. 存储token
  setToken(token, refresh_token) {
    // 2.1 处理token和refresh_token
    token = 'Bearer ' + token
    refresh_token = 'Bearer ' + refresh_token
    // 2.2 全局存储token和refresh_token
    this.token = token
    this.refresh_token = refresh_token
    // 2.3 本地存储token和refresh_token
    wx.setStorageSync('token', token)
    wx.setStorageSync('refresh_token', refresh_token)
  },
})
